<?php
/**
 * Created by PhpStorm.
 * User: dy
 * Date: 2019/12/19
 * Time: 16:49
 */
namespace app\admin\model;
use app\common\model\BaseModel;
use think\Request;

class Admin extends BaseModel
{
    protected $type = [
        'login_time'=>'timestamp',
        'last_login_time'=>'timestamp',
        'status'=>'integer',
        'parent_id'=>'integer',
        'login_count'=>'integer'
    ];
    public function setPasswordAttr($value){
        if(empty($value)){
            return $value;
        }
        return password_hash($value,PASSWORD_DEFAULT);
    }
    public function ParentAdmin(){
        return $this->belongsTo('admin','parent_id');
    }
    public function buildParams(Request $request){
        $where = [];
        $page = $request->get('page',DATA_NORMAL);
        $limit = $request->get('limit',LIMIT);
        $order = $request->get('order','ASC');
        $sort = $request->get('sort','id');
        $search_data = $request->get('search_data');
        if($search_data){
            $where[] = [
                'username|nickname|phone|email',
                'like',
                "%{$search_data}%"
            ];
        }
        return [$where,$sort,$order,$page,$limit];
    }
}